package cn.kgc.biz.service;

import cn.kgc.biz.vo.PageVo;
import cn.kgc.vo.*;
import cn.kgc.dto.Dto;
import cn.kgc.mapper.CommentMapper;
import cn.kgc.util.DtoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    @Override
    public Dto getHotelScore(Long hotelId) {
        HotelAvgScore hotelAvgScore=commentMapper.getHotelAvgScoreByHotelId(hotelId);
        return DtoUtil.getSuccesWithData(hotelAvgScore);
    }

    @Override
    public Dto getCount(Long hotelId) {
        CommentCountVo commentCountVo=commentMapper.getCommentCountByHotelId(hotelId);
        return DtoUtil.getSuccesWithData(commentCountVo);
    }

    @Override
    public Dto getCommentList(CommentPageVo commentPageVo) {
        PageHelper.startPage(commentPageVo.getPageNo(),commentPageVo.getPageSize());
        List<CommentVo> commentVoList=commentMapper.getCommentList(commentPageVo);
        PageInfo<CommentVo> pageInfo=new PageInfo<>(commentVoList);
        long total=pageInfo.getTotal();
        long pageNum=pageInfo.getPageNum();
        long pages=pageInfo.getPages();
        long pageSize=pageInfo.getPageSize();
        PageVo pageVo=new PageVo();
        pageVo.setBeginPos(0);
        pageVo.setCurPage(pageNum);
        pageVo.setPageSize(pageSize);
        pageVo.setTotal(total);
        pageVo.setPageCount(pages);
        pageVo.setRows(pageInfo.getList());
        return DtoUtil.getSuccesWithData(pageVo);
    }

    @Override
    public Dto gettImg(Long commentId) {
        List<CommentImgVo> commentImgVoList=commentMapper.getCommentImgByCommentId(commentId);
        return DtoUtil.getSuccesWithData(commentImgVoList);
    }
}
